package com.cm.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cm.common.core.Result;
import com.cm.order.entity.Address;
import com.cm.order.entity.dto.AddressKeyWordDTO;
import com.cm.order.entity.vo.AddressVO;
import com.cm.order.mapper.AddressMapper;
import com.cm.order.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @类 名:AddressController </br>
 * @描 述:<br>
 * @日 期:2020/9/25 17:08 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    AddressService addressService;

    @PostMapping("/insert")
    public Result insert (@RequestBody Address address){
        if (addressService.insert(address)) {
            return Result.success("收货地址添加成功");
        }
        return Result.fail(address.getUserId() + "已存在！");
    }

    @PostMapping("/delete")
    public Result delete(Long id) {
        if (addressService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Address address) {
        if (addressService.update(address)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @PostMapping("/list")
    public Result getAddress(@RequestBody AddressKeyWordDTO addressKeyWordDTO) {
        IPage<AddressVO> ordersIPage = addressService.getAddresstList(addressKeyWordDTO);
        return Result.success(ordersIPage);
    }

    @GetMapping("/getById")
    public Result getById(Long id){
        Address address= addressMapper.selectById(id);
        return Result.success(address);
    }
    @PostMapping("/getByUser")
    public Result getByUserId(Long id){
        Address address=addressService.getByUserId(id);
        return Result.success(address);
    }
}
